package com.bsh.controller.account;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.api.ApiUtils;
import com.bsh.dto.AccountNoticeQryDto;
import com.bsh.dto.UserEditDto;
import com.bsh.service.AccountChangeAuditService;
import com.bsh.service.AccountService;
import com.bsh.shiro.UserContext;
import com.bsh.utils.PageJosnMessage;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

/**
 * 后台变动账户最终审核
 * 
 * @author gsh
 *
 */
@Controller
public class AccountChangeAuditController {
	private static Logger log = LoggerFactory.getLogger(AccountChangeAuditController.class);

	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	AccountChangeAuditService changeAuditService;
	@Autowired
	private AccountService accountServ;

	/**
	 * 设置查询默认时间范围
	 */
	private void setDefaultDateTimeRange(AccountNoticeQryDto dto){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if(ApiUtils.objIsEmpty(dto.getCreateTime())&&ApiUtils.objIsEmpty(dto.getCreateTime1())){
        	  Calendar cal=Calendar.getInstance();
    		  cal.setTime(new Date());	
    		  cal.add(Calendar.DAY_OF_MONTH,-3);
    		  dto.setCreateTime(sdf.format(cal.getTime()));
    		  dto.setCreateTime1(sdf.format(new Date()));
        }        
	}
	
	/**
	 * 账户变动最终审核--列表
	 */
	@RequestMapping(value = "/account/change/audit/list")
	public ModelAndView auditChangeList(@ModelAttribute AccountNoticeQryDto dto) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/account/auditChangeList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		setDefaultDateTimeRange(dto);
		
		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = changeAuditService.getPage(pager, dto);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("dto", dto == null ? new UserEditDto() : dto);

		return mv;
	}

	/**
	 * 账户变动最终审核--拒绝
	 * 
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/account/change/audit/refuse/{id}")
	public PageJosnMessage refuse(@PathVariable(value = "id") Integer changeId) {
		String res = accountServ.auditAccountChange(changeId, UserContext.getLoginUser().getId(), false);
		String status = res.substring(0, 2);
		String msg=res.substring(2);
		msg = "ok".equals(status) ? "保存成功！" : "保存失败,"+msg;

		PageJosnMessage pmsg = new PageJosnMessage();

		pmsg.putData("status", "ok".equals(status)?"1":"0");
		pmsg.putData("msg", msg);
		return pmsg;
	}

	/**
	 * 账户变动最终审核--通过
	 * 
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/account/change/audit/pass/{id}")
	public PageJosnMessage pass(@PathVariable(value = "id") Integer changeId) {
		String res = accountServ.auditAccountChange(changeId, UserContext.getLoginUser().getId(), true);
		String status = res.substring(0, 2);
		String msg=res.substring(res.indexOf(":")+1);
		msg = "ok".equals(status) ? "保存成功！" : msg;

		PageJosnMessage pmsg = new PageJosnMessage();

		pmsg.putData("status", "ok".equals(status)?"1":"0");
		pmsg.putData("msg", msg);
		return pmsg;
	}
	
	/**
	 * 账户变动最终审核--批量通过
	 * 
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/account/change/audit/pass/batch/{idList}")
	public PageJosnMessage passBatch(@PathVariable(value = "idList") String idList) {
		String [] ary=idList.split("_");
		boolean hasErr=true;
		for (String str : ary) {
			Integer changeId=Integer.parseInt(str);
			String res = accountServ.auditAccountChange(changeId, UserContext.getLoginUser().getId(), true);
			if(!"ok".equals(res.substring(0, 2)))hasErr=false;
		}
		PageJosnMessage pmsg = new PageJosnMessage();

		pmsg.putData("status", hasErr?"1":"0");
		pmsg.putData("msg", hasErr?"保存成功！":"部分数据无法通过，请检查数据状态及账户资金余额！");
		return pmsg;
	}
	

}
